package com.clock.ui.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.clock.photowall.R;
import com.clock.ui.view.waterfall.DynamicHeightImageView;
import com.clock.ui.view.waterfall.StaggeredGridView;

public class WaterfallActivity extends Activity {

	List<Integer> water_items = new ArrayList<Integer>();
	StaggeredGridView waterfall;
	int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 8) ;
	LruCache<Integer, Bitmap> bitmapCache = new LruCache<Integer, Bitmap>(cacheSize){
		@Override
		protected int sizeOf(Integer key, Bitmap value) {
			return value.getHeight() * value.getRowBytes();
		};
	};
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_waterfall);
		setTitle("AndroidStaggeredGrid");
		
		int[] waterfall_images = new int[]{R.drawable.clock , R.drawable.ic_launcher };
		Random random = new Random();
		for (int i = 0; i < 100; i++) {
			water_items.add(waterfall_images[random.nextInt(2)]);
		}
		
		waterfall = (StaggeredGridView) findViewById(R.id.sg_waterfall);
		waterfall.setAdapter(new WaterFallAdapter());//API 11
	}

	
	class WaterFallAdapter extends BaseAdapter{
		SparseArray<Double> mPositionHeightRatios = new SparseArray<Double>();
		Random mRandom = new Random();
		
		@Override
		public int getCount() {
			if (water_items == null) {
				return 0;
			}
			return water_items.size();
		}

		@Override
		public Object getItem(int position) {
			return water_items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			WaterHolder viewHolder ;
			if (convertView == null) {
				convertView = View.inflate(WaterfallActivity.this, R.layout.waterfall_item, null);
				viewHolder = new WaterHolder();
				viewHolder.imageHolder = (DynamicHeightImageView) convertView.findViewById(R.id.water_image);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (WaterHolder) convertView.getTag();
			}
			//viewHolder.imageHolder.setImageResource(water_items.get(position));
			Bitmap bitmap = bitmapCache.get(water_items.get(position));
			if (bitmap == null) {
				bitmap = BitmapFactory.decodeResource(getResources(), water_items.get(position));
				bitmapCache.put(water_items.get(position), bitmap);
			}
			viewHolder.imageHolder.setImageBitmap(bitmap);
			 double positionHeight = getPositionRatio(position);
			 viewHolder.imageHolder.setHeightRatio(positionHeight);
			return convertView;
		}
		
		private double getPositionRatio(final int position) {
		       double ratio = mPositionHeightRatios.get(position, 0.0);
		       // if not yet done generate and stash the columns height
		       // in our real world scenario this will be determined by
		       // some match based on the known height and width of the image
		       // and maybe a helpful way to get the column height!
		       if (ratio == 0) {
		           ratio = getRandomHeightRatio();
		           mPositionHeightRatios.append(position, ratio);
		       }
		       return ratio;
		 }

		 private double getRandomHeightRatio() {
		       return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
		 }
	}
	
	static class WaterHolder{
		DynamicHeightImageView imageHolder;
	}
}
